package com.bss.framework;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bss.ajax.AjaxCommand;
import com.bss.ajax.BackupCommand;
import com.bss.ajax.FindAttendentCommand;

public class AjaxServlet extends HttpServlet {
	
	private HashMap<String, AjaxCommand> commands = new HashMap<String, AjaxCommand>();

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * initialization method for servlet
	 */
	public void init() {
		commands.put("findAttendent", new FindAttendentCommand());
		commands.put("backup", new BackupCommand());
	}

	/**
	 * The doget method of the servlet
	 * 
	 * @param request
	 *            HttpServletRequest object
	 * @param response
	 *            HttpServletResponse object
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,
			java.io.IOException {

		response.getWriter().println("NO OUTPUT");
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		
		try {
					
		Map<String,String[]> map = request.getParameterMap();
		
		String result = commands.get(map.get("operation")[0]).execute(map);
		
		if (result != null)
			response.getWriter().write(result);
		
		} catch (Exception e) {
			FrontController.debug("Error while finding attendents: "+e.fillInStackTrace());
			response.getWriter().write("ERROR");
		}
	}
	
}